banner.admin = liwe.module ( "banner.admin" );

banner.admin.cbacks = {
	"container_choose": null
};


banner.admin.admin_man_grp = null;

banner.admin._admin_manager_init_stats = function ( dest_div )
{
	if ( ! banner.admin.admin_man_stats )
	{
		banner.admin.admin_man_stats = new AdminManager ( { 
			'dest_res': dest_div,
			'list_action': "banner.admin.counters_list",
			'del_action': 'banner.admin.counters_del'
			/*,'mod_menu_actions': [
				{ func: "banner.admin.add_categ('block_main')", img_src: '/modules/news/gfx/nuova_categoria.png', label: 'Nuova Categoria' },
				{ func: "banner.admin.list_categs('block_main')", img_src: '/modules/news/gfx/lista_categorie.png', label: 'Lista Categorie' },
				{ func: "banner.admin.add_sottocateg('block_main')", img_src: '/modules/news/gfx/nuova_categoria.png', label: 'Nuova sottocategoria' },
				{ sep: 1 },
				{ func: "banner.admin.set('block_main')", img_src: '/modules/news/gfx/nuova_news.png', label: 'Nuova News' }
			]*/
		} );

		banner.admin.admin_man_stats.set_row_fields ( 
					[
						[ "ID", "id" ],
						[ "Immagine", "_img" ],
						[ "Contenitore", "container" ],
						[ "Gruppo", "grp" ],
						[ "URL", "url_click" ],
						[ "Num. visualizz.", "count_view" ],
						[ "Num. click", "count_click" ]
					]
					);


		banner.admin.admin_man_stats.cbacks [ 'row_manip' ] = banner.admin.banner_list_row_manip;
		//banner.admin.admin_man_stats.cbacks [ 'edit_item' ] = function () { void(0); };
		//banner.admin.admin_man_stats.cbacks [ 'del_item' ] = function () { void(0); };
	}
};

banner.admin.stat_list_row_manip = function ( ds, row )
{
	row [ "_id" ] = row [ "id" ];
};

banner.admin.stats_list = function ( dest )
{
	banner.admin.admin_man_stats.show_list ();
};


banner.admin._admin_manager_init_banners = function ( dest_div )
{
	if ( ! banner.admin.admin_man_banners )
	{
		banner.admin.admin_man_banners = new AdminManager ( { 
			'dest_res': dest_div,
			'list_action': "banner.admin.banner_list2",
			'del_action': 'banner.admin.banner_del'
			/*,'mod_menu_actions': [
				{ func: "banner.admin.add_categ('block_main')", img_src: '/modules/news/gfx/nuova_categoria.png', label: 'Nuova Categoria' },
				{ func: "banner.admin.list_categs('block_main')", img_src: '/modules/news/gfx/lista_categorie.png', label: 'Lista Categorie' },
				{ func: "banner.admin.add_sottocateg('block_main')", img_src: '/modules/news/gfx/nuova_categoria.png', label: 'Nuova sottocategoria' },
				{ sep: 1 },
				{ func: "banner.admin.set('block_main')", img_src: '/modules/news/gfx/nuova_news.png', label: 'Nuova News' }
			]*/
		} );

		banner.admin.admin_man_banners.set_row_fields ( 
					[
						[ "ID", "id" ],
						[ "Immagine", "_img" ],
						[ "Frequenza", "issue_rate" ],
						[ "Popup", "popup" ],
						[ "URL", "url_click" ]
					]
					);


		banner.admin.admin_man_banners.cbacks [ 'row_manip' ] = banner.admin.banner_list_row_manip;
		banner.admin.admin_man_banners.cbacks [ 'edit_item' ] = function ( id ) { banner.admin.banner_edit ( "am_footer", id ); };
		banner.admin.admin_man_banners.cbacks [ 'del_item' ] = banner.admin.banner_del;
	}
};

banner.admin.banner_list_row_manip = function ( ds, row )
{
	row [ "_id" ] = row [ "id" ];
};

banner.admin.banner_list = function ( dest )
{
	banner.admin.admin_man_banners.show_list ();
};


banner.admin._admin_manager_init_cnts = function ( dest_div )
{
	if ( ! banner.admin.admin_man_cnts )
	{
		banner.admin.admin_man_cnts = new AdminManager ( { 
			'dest_res': dest_div,
			'list_action': "banner.admin.container_list2",
			'del_action': 'banner.admin.container_del'
			/*,'mod_menu_actions': [
				{ func: "banner.admin.add_categ('block_main')", img_src: '/modules/news/gfx/nuova_categoria.png', label: 'Nuova Categoria' },
				{ func: "banner.admin.list_categs('block_main')", img_src: '/modules/news/gfx/lista_categorie.png', label: 'Lista Categorie' },
				{ func: "banner.admin.add_sottocateg('block_main')", img_src: '/modules/news/gfx/nuova_categoria.png', label: 'Nuova sottocategoria' },
				{ sep: 1 },
				{ func: "banner.admin.set('block_main')", img_src: '/modules/news/gfx/nuova_news.png', label: 'Nuova News' }
			]*/
		} );

		banner.admin.admin_man_cnts.set_row_fields ( 
					[
						[ "ID", "id" ],
						[ "Nome", "name" ],
						[ "Frequenza", "issue_rate" ],
						[ "Larghezza", "width" ],
						[ "Altezza", "height" ]
					]
					);


		banner.admin.admin_man_cnts.cbacks [ 'row_manip' ] = banner.admin.container_list_row_manip;
		banner.admin.admin_man_cnts.cbacks [ 'edit_item' ] = function ( id ) { banner.admin.cont_edit ( "am_footer", id ); };
		banner.admin.admin_man_cnts.cbacks [ 'del_item' ] = banner.admin.cont_del;
	}
};

banner.admin.container_list_row_manip = function ( ds, row )
{
	row [ "_id" ] = row [ "id" ];
};

banner.admin.container_list = function ( dest )
{
	banner.admin.admin_man_cnts.show_list ();
};

// {{{ GROUPS MANAGEMENT
banner.admin._admin_manager_init_groups = function ( dest_div )
{
	if ( ! banner.admin.admin_man_grp )
	{
		banner.admin.admin_man_grp = new AdminManager ( { 
			'dest_res': dest_div,
			'list_action': "banner.admin.group_list",
			'del_action': 'banner.admin.group_del'
			/*,'mod_menu_actions': [
				{ func: "banner.admin.add_categ('block_main')", img_src: '/modules/news/gfx/nuova_categoria.png', label: 'Nuova Categoria' },
				{ func: "banner.admin.list_categs('block_main')", img_src: '/modules/news/gfx/lista_categorie.png', label: 'Lista Categorie' },
				{ func: "banner.admin.add_sottocateg('block_main')", img_src: '/modules/news/gfx/nuova_categoria.png', label: 'Nuova sottocategoria' },
				{ sep: 1 },
				{ func: "banner.admin.set('block_main')", img_src: '/modules/news/gfx/nuova_news.png', label: 'Nuova News' }
			]*/
		} );

		banner.admin.admin_man_grp.set_row_fields ( 
					[
						[ "ID", "id" ],
						[ "Nome", "name" ],
						[ "Abilitato", "_enabled" ],
						[ "Scadenza", "_expiration" ]
					]
					);


		banner.admin.admin_man_grp.cbacks [ 'row_manip' ] = banner.admin.group_list_row_manip;
		banner.admin.admin_man_grp.cbacks [ 'edit_item' ] = function ( id ) { banner.admin.group_edit ( "am_footer", id ); };
		banner.admin.admin_man_grp.cbacks [ 'del_item' ] = banner.admin.group_del;
	}
};


banner.admin.group_list_row_manip = function ( ds, row )
{
	row [ "_id" ] = row [ "id" ];

	row [ "_enabled" ] = row [ "enabled" ] ? "S&igrave;" : "No";
	if ( ! row [ "expiration" ] ) row [ "_expiration" ] = "Nessuna";
	else row [ "_expiration" ] = liwe.utils.date2str ( row [ 'expiration' ], 1 );
};


banner.admin.group_edit = function ( dest, id )
{
	banner.admin._cur_dest = dest;

	var is_edit = typeof id != "undefined";

	function _create_form ( values )
	{
		if ( ! values ) values = {};

		var f = new liwe.form.instance ( "banner_group" );
		f.events [ "complete" ] = banner.admin._group_edit_complete;

		f.hidden ( "action", is_edit ? "banner.admin.group_change" : "banner.admin.group_add" );
		if ( is_edit ) f.hidden ( "id_group", id );

		f.text ( { label: "Nome", name: "name", mandatory: true, value: values.get ( "name" ) } );
		f.checkbox ( { label: "Abilitato", name: "enabled", checked: values.get ( "enabled", true ) } );
		f.hidden ( "expiration", "" );

		f.submit ( is_edit ? "MODIFICA" : "CREA" );

		f.set ( dest );
	}

	if ( is_edit )
	{
		liwe.AJAX.easy ( { action: "banner.admin.group_get", id_group: id }, function ( v )
		{
			var group = v [ "group" ];
			_create_form ( group );
		} );
	}
	else
		_create_form ( dest );
};


banner.admin._group_edit_complete = function ( v )
{
	banner.admin.group_list ( banner.admin._cur_dest );
};


banner.admin.group_list = function ( dest )
{
	banner.admin.admin_man_grp.show_list ();
};


banner.admin.group_del = function ( id )
{
	if ( ! confirm ( "Eliminare veramente il gruppo?" ) ) return;

	liwe.AJAX.easy ( { action: "banner.admin.group_del", ids: id }, function ( v )
	{
		banner.admin.group_list ( banner.admin._cur_dest );
	} );
};
// }}}

// {{{ CONTAINERS MANAGEMENT
banner.admin.cont_edit = function ( dest, id )
{
	banner.admin._cur_dest = dest;

	var is_edit = typeof id != "undefined";

	function _create_form ( values )
	{
		if ( ! values ) values = {};

		var f = new liwe.form.instance ( "banner_container" );
		f.events [ "complete" ] = banner.admin._cont_edit_complete;

		f.hidden ( "action", is_edit ? "banner.admin.container_change" : "banner.admin.container_add" );
		if ( is_edit ) f.hidden ( "id_cont", id );

		f.text ( { label: "Nome", name: "name", mandatory: true, value: values.get ( "name" ) } );
		f.text ( { label: "Frequenza", name: "issue_rate", value: values.get ( "issue_rate" ) } );
		f.text ( { label: "Larghezza", name: "width", value: values.get ( "width" ) } );
		f.text ( { label: "Altezza", name: "height", value: values.get ( "height" ) } );

		f.submit ( is_edit ? "MODIFICA" : "CREA" );

		f.set ( dest );
	}

	if ( is_edit )
	{
		liwe.AJAX.easy ( { action: "banner.admin.container_get", id_cont: id }, function ( v )
		{
			var cont = v [ "container" ];
			_create_form ( cont );
		} );
	}
	else
		_create_form ( dest );
};


banner.admin._cont_edit_complete = function ( v )
{
	banner.admin.container_list ( banner.admin._cur_dest );
};


banner.admin.cont_list = function ( dest )
{
	banner.admin._cur_dest = dest;

	liwe.AJAX.easy ( { action: "banner.admin.container_list" }, function ( v )
	{
		var gs = v [ "containers" ];

		var i, l = gs.length;
		var s = banner.admin.templates [ "cont_list_start" ];
		for ( i = 0; i < l; i ++ )
		{
			var g = gs [ i ];
			g [ "_dest" ] = dest;
			g [ "_pos" ] = i;

			s += String.formatDict ( banner.admin.templates [ "cont_list_row" ], g );
		}
		s += banner.admin.templates [ "cont_list_end" ];

		$ ( dest, s );
	} );
};


banner.admin.cont_del = function ( id )
{
	if ( ! confirm ( "Eliminare veramente il contenitore?" ) ) return;

	liwe.AJAX.easy ( { action: "banner.admin.container_del", ids: id }, function ( v )
	{
		banner.admin.cont_list ( banner.admin._cur_dest );
	} );
};
// }}}

// {{{ BANNER MANAGEMENT
banner.admin._init_tabs = function ( dest_div )
{
	var s = new String.buffer ();
	var res;

	if ( ! banner.admin.tabs ) banner.admin.tabs = new OS3Tabs.instance ( 'banner-tabs' );

	banner.admin.tabs.clear ();

	s.add ( banner.admin.templates [ 'env_start' ] );
	s.add ( banner.admin.templates [ 'tabs_start' ] );

	s.add ( String.formatDict ( banner.admin.templates [ 'tabs_tab' ], { "tab_name" : "banner_form" } ) );
	banner.admin.tabs.add ( 'Banner', 'banner_form' );

	s.add ( String.formatDict ( banner.admin.templates [ 'tabs_tab' ], { "tab_name" : "banner_media" } ) );
	banner.admin.tabs.add ( 'Media', 'banner_media' );

	s.add ( banner.admin.templates [ 'tabs_end' ] ); 
	s.add ( banner.admin.templates [ 'env_end' ] );

	res = s.toString ();

	$( dest_div, res );
};


banner.admin.banner_edit = function ( dest, id )
{
	banner.admin._cur_dest = dest;

	var is_edit = typeof id != "undefined";

	function _create_form ( values )
	{
		if ( ! values ) values = {};

		banner.admin._init_tabs ( dest );

		media_manager.admin.render ( "banner_media", "banner", id );

		liwe.AJAX.easy ( { action: "banner.admin.group_list", mode: "list" }, function ( v )
		{
			var groups = v [ "groups" ];
			liwe.AJAX.easy ( { action: "banner.admin.container_list" }, function ( v )
			{
				var containers = v [ "containers" ];

				var group_list = [ { label: "(nessuno)", value: "" } ];
				var cont_list = [];
				var i, l;

				l = groups.length;

				for ( i = 0; i < l; i ++ )
					group_list.push ( { label: groups [ i ] [ "name" ], value: groups [ i ] [ "id" ] } );

				l = containers.length;
				for ( i = 0; i < l; i ++ )
					cont_list.push ( { label: containers [ i ] [ "name" ], value: containers [ i ] [ "id" ] } );

				var f = banner.admin._form = new liwe.form.instance ( "banner_file" );
				f.events [ "complete" ] = banner.admin._banner_edit_complete;

				f.hidden ( "action", is_edit ? "banner.admin.banner_change" : "banner.admin.banner_add" );
				if ( is_edit ) f.hidden ( "id_banner", id );

				f.text ( { label: "URL click", name: "url_click", value: values.get ( "url_click" ) } );
				f.checkbox ( { label: "Mostra come popup?", name: "popup", checked: values.get ( "popup" ) } );
				f.text ( { label: "Frequenza", name: "issue_rate", value: values.get ( "issue_rate" ) } );
				f.text ( { label: "Tempo di visualizzazione", name: "view_time", value: values.get ( "view_time" ) } );

				var dct = {
					label: "Contenitore", name: "id_container", force_select: true, mandatory: true,
					value: values.get ( "id_container" ),
					options: cont_list
				};
				if ( banner.admin.cbacks [ "container_choose" ] ) dct [ "nonl" ] = true;
				f.select ( dct );

				if ( banner.admin.cbacks [ "container_choose" ] )
					f.button ( { value: "Scegli", onclick: "banner.admin._container_choose()" } );

				f.select ( { label: "Gruppo", name: "id_group",	value: values.get ( "id_group" ),
					options: group_list } );

				f.submit ( is_edit ? "MODIFICA" : "CREA" );

				f.set ( "banner_form" );

				banner.admin.tabs.render ();
				banner.admin.tabs.send_event ( 'banner_form', 'click' );
			} );
		} );
	}

	if ( is_edit )
	{
		liwe.AJAX.easy ( { action: "banner.admin.banner_get", id_banner: id }, function ( v )
		{
			var banner = v [ "banner" ];
			_create_form ( banner );
		} );
	}
	else
		_create_form ( dest );
};


banner.admin._banner_edit_complete = function ( v )
{
	banner.admin.banner_list ( banner.admin._cur_dest );
};

/*
banner.admin.banner_list = function ( dest )
{
	banner.admin._cur_dest = dest;

	liwe.AJAX.easy ( { action: "banner.admin.banner_list" }, function ( v )
	{
		var gs = v [ "banners" ];

		var i, l = gs.length;
		var s = banner.admin.templates [ "banner_list_start" ];
		for ( i = 0; i < l; i ++ )
		{
			var g = gs [ i ];
			g [ "_dest" ] = dest;
			g [ "_pos" ] = i;

			s += String.formatDict ( banner.admin.templates [ "banner_list_row" ], g );
		}
		s += banner.admin.templates [ "banner_list_end" ];

		$ ( dest, s );
	} );
};
*/

banner.admin.banner_del = function ( id )
{
	if ( ! confirm ( "Eliminare veramente il banner?" ) ) return;

	liwe.AJAX.easy ( { action: "banner.admin.banner_del", ids: id }, function ( v )
	{
		banner.admin.banner_list ( banner.admin._cur_dest );
	} );
};


banner.admin._container_choose = function ()
{
	banner.admin.cbacks [ "container_choose" ] ( function ( cnt )
	{
		banner.admin._form.set_value ( "id_container", cnt );
	} );
};
// }}}

// {{{ STATS
/*
banner.admin.stats_count = function ( dest )
{
	//liwe.AJAX.easy ( { action: "banner.admin.get_stats" }, function ( v ) { console.debug ( v ); } );

	liwe.AJAX.easy ( { action: "banner.admin.get_counters" }, function ( v )
	{
		var items = v [ "counters" ];
		var s = banner.admin.templates [ "stat_header" ];

		var i, l = items.length;
		for ( i = 0; i < l; i ++ )
		{
			s += String.formatDict ( banner.admin.templates [ "stat_row" ], items [ i ] );
		}

		s += banner.admin.templates [ "stat_footer" ];

		$ ( dest ).innerHTML = s;
	} );
};
*/


banner.admin.stats_event = function ( dest )
{
	$ ( dest, '<div id="stats_form"></div><div id="stats_hidden_form_cnt" style="display: none"></div>' );

	var f = new liwe.form.instance ( "banner_stats", "/modules/banner/down_stats.pyhp" );

	f.calendar ( { label: "Data iniziale", name: "start" } );
	f.calendar ( { label: "Data finale", name: "end" } );

	f.button ( { name: "btn", value: "Ottieni", onclick: "banner.admin._get_stats_event()" } );

	f.set ( "stats_form" );

	banner.admin._form = f;
};


banner.admin._get_stats_event = function ()
{
	var vals = banner.admin._form.get_values ();

	$ ( "stats_hidden_form_cnt" ).innerHTML =
		'<form id="stats_hidden_form" method="post" action="/modules/banner/down_stats.pyhp">' +
		'<input type="hidden" name="start" value="' + vals [ "start" ] + '" />' +
		'<input type="hidden" name="end" value="' + vals [ "end" ] + '" />' +
		'</form>';

	$ ( "stats_hidden_form" ).submit ();
};

banner.admin.init = function ( dest_div )
{
	/*if ( ! banner.admin.templates )
	{
		liwe.AJAX.easy ( { action: "banner.ajax.get_templates" }, function ( v ) {
			banner.admin.templates = v [ 'templates' ];
			banner.admin._admin_manager_init ( dest_div );
		} );
	}*/
	banner.admin._admin_manager_init_groups ( dest_div );
	banner.admin._admin_manager_init_cnts ( dest_div );
	banner.admin._admin_manager_init_banners ( dest_div );
	banner.admin._admin_manager_init_stats ( dest_div );
	var _acts = [
			{ func : function () { banner.admin.admin_man_stats.set_dest_div ( 'system-content' ); banner.admin.counters_list (); },
			  img : '' , label : 'Lista contatori', "class" : '' },
			{ func : function () { banner.admin.admin_man_banners.set_dest_div ( 'system-content' ); banner.admin.banner_list2 (); },
			  img : '' , label : 'Lista banner', "class" : '' },
			{ func : function () { banner.admin.admin_man_cnts.set_dest_div ( 'system-content' ); banner.admin.container_list2 (); },
			  img : '' , label : 'Lista containers', "class" : '' },
			{ func : function () { banner.admin.admin_man_grp.set_dest_div ( 'system-content' ); banner.admin.group_list (); },
			  img : '' , label : 'Lista banner', "class" : '' }
		    ];
	banner.admin.register ( _acts );
};
// }}}

